#!/usr/bin/env python

import sys


#load 0
load_instr=('ldr','ldrvst','ldrmi','ldrmisb','ldrmish','ldrmit','ldrge','ldrgt','ldrgtsh','ldrgtb','ldmvcia','ldmltda','ldmltdb','ldmltia','ldmleia','ldmltia','ldmleia','ldmlsib','ldmlsia','ldmeqda','ldmhiia','ldmeqia','ldmgeia','ldmgtia','ldmnedb','ldmnvda','ldmia','ldrb','ldmneia','ldrnvb','ldrne','ldrneb','ldreq','ldreqbt','ldreqh','ldreqsh','ldrneh','ldrnesh','ldreqb','lfm','ldreqd','ldmccia','ldmccib','ldmmiia','ldmmidb','ldmmiib','ldmcsia','ldrls','ldrlsb','ldrle','ldrlt','ldrh','ldrcs','ldrhi','ldrhib','ldmdb','ldmda','ldmib','ldmeqdb','ldrcc','ldrcch','ldrccb','ldrsh','ldrsb','ldrplb')

#store 1
store_instr=('str','stmccib','stmgedb','stmgeia','stmgtda','stmgtdb','stmledb','stmlsda','stmlsib','stmeqia','stmdb','stmeqdb','stmda','stmneia','stmhiia','strb','streqh','streqb','sfm','strmi','strmib','strgt','strgtb','strge','strgeb','strlt','strltbt','strlsb','strls','stfmid','stfgtd','stfned','stfled','stfd','stfs','stfeqd','strneb','strneh','strvs','strvsbt','streq','streqd','stmia','strne','stmib','strcsh','strccb','strcc','strcs','strcch','strle','strh','strhi','strhib','strpl')

#cond_branch 2
cond_instr=('beq','bne','bmi','bpl','ble','blge','blgt','bge','blne','bcs','bcc','bls','blt','bhi','blmi','bleq','bgt','blvs','blvc','blcc','bvc')

#uncond_branch 3
uncond_instr=('b', 'bl','bx','blx')

#int_ALU 4
int_instr=('add','addmi','addhi','adds','addcs','addcc','addgt','addgts','addge','addls','addle','addlt','subcc','cmple','cmpmi','cmpls','cmpge','mov','mvn','mvngt','mvnls','mvnlt','mvnle','mvneq','mvnne','mvnpl','mvnhi','mvncc','mvncs','adc','adcmis','adceq','adcges','adcgt','adccss','and','andeq','andgt','andgts','cmp','sub','subge','subgt','subne','ands','biceq','bichi','bicne','bic','bicmis','bicgts','bics','bicls','movs','movmi','movpl','subs','rsb','rsbs','rsbne','rsbeq','rsbgt','rsblt','rsble','rsbls','rsbhi','rsbcc','rsbcs','movne','smlawbmi','muleq','smull','umull','moveq','movhi','andcs','andcc','andlt','andle','andls','andmi','andmis','orr','orrhi','orrs','orrle','orrls','orrlt','eor','eors','eorgt','eorgts','eorne','eormis','eormi',
'teq','teqls','teqcc','tst','tstmi','tstne','subeq','submi','submis','subhi','sublt','suble','subls','andne','orrne','cmn','cmnmi','cmnne','cmnnv','cmnvc','addeq','addne','orreq','movcc','movnes','sbc','sbceqs','sbcmi','movcs','movls','movlt','andhi','mul','mulgt','muls','mulne','mulhi','orrgt','movle','movgt','movge','orrcc','orrcs','subcs','cmphi','cmpeq','cmpcc','cmpne','cmpvc','rsbmi','rsbmis','mla','rsc','rscles','rscgt','rscmi','swp','swpne','mlagts')

#fp_ALU 5
fp_instr=('ldfeqs','ldfned','ldfd','ldfmid','ldfmip','ldfeqd','ldfgtd','ldfled','mvfd','mvfled','mvfltd','mvfs','mvfeqs','mvfeqd','mvfned','mvfnes','fltd','dvfd','dvfned','dvfgtd','cmf','cmfe','mvfmid','mvfgtd','ldfs','mnf','mnfs','mnfpld','mnfmid','mnfled','mnfeqd','mufgtd','mnfd','mufmid','mufeqd','mnfned','mnfnes','adfs','adfled','adfgtd','adfned','adfeqd','adfd','mufd','sufd','sufled','sufgtd','rfs','wfs','cfcmpsle','absd','absvs','rsfd','rsfgtd','adfmid','sufeqd','fnmulsvc','cnf','cnfe','rdf','rdfd','sqtd','fixz','fixeqz','fldmiaslt','fltnes','flts','fmls','fmleqs','fdvs','fdvnes')

#other 6
other_instr=('swi','swivc','swinv','swicc','cdp2','cdpge','cdphi','cdpvs','nop','stc','stcpll','stcgtl','swilt','mrccc','mcrcs','mrcge','swieq','ldcccl','ldcgtl','ldccc','ldclsl','cdpmi','stceql','mcrge','mrrcvc','mrrcle')

#Not instruction 7
none_instr=('format', 'section')

load_count=0
store_count=0
cond_count=0
uncond_count=0
int_count=0
fp_count=0
other_count=0
none_count=0


if len(sys.argv) < 2:
    print "Usage: count_instr.py [filename]"
    exit(1)

fp=open(sys.argv[1], "r")
print "Parsing '"+sys.argv[1]+"' ..."

one_instr=fp.readline()
one_instr=one_instr.rstrip()
while len(one_instr) > 0:
    if one_instr in load_instr:
        #print "One load"
        load_count+=1
    elif one_instr in store_instr:
        #print "One store"
        store_count+=1
    elif one_instr in cond_instr:
        #print "One cond"
        cond_count+=1
    elif one_instr in uncond_instr:
        #print "One uncond"
        uncond_count+=1
    elif one_instr in int_instr:
        #print "One int"
        int_count+=1
    elif one_instr in fp_instr:
        #print "One fp"
        fp_count+=1
    elif one_instr in other_instr:
        #print "One other"
        other_count+=1
    elif one_instr in none_instr:
        #print "One None"
        none_count+=1
    else:
        print "Unhandled instr: "+one_instr
        #exit(1)
    one_instr=fp.readline()
    one_instr=one_instr.rstrip()

